﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccessLayer;
using BusinessLogicLayer;
using System.Data.SqlClient;
using System.IO;
using System.Drawing;
using System.Data;

namespace ChamCong.Admin
{
    public partial class XemLuong : System.Web.UI.Page
    {
        CT_LuongThangBLL ctlBLL = new CT_LuongThangBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                txtNam.Text = DateTime.Now.Year.ToString();
                drThang.SelectedValue = DateTime.Now.Month.ToString();

                //LoadReport(11, 2012, "", 0);
            }
        }

        protected void LoadReport(int _thang, int _nam, string _hoten, int _mapb)
        {
            var source =
                new Microsoft.
                Reporting.
                WebForms.
                ReportDataSource("dsBangLuong", new CT_LuongThangBLL().LayBangLuong2(_thang, _nam, _mapb, _hoten));

            repv.LocalReport.ReportPath = "Report/BangLuongReport.rdlc";
            repv.LocalReport.DataSources.Clear();
            repv.LocalReport.DataSources.Add(source);
            repv.LocalReport.Refresh();
        }

        protected void btnXem_Click(object sender, EventArgs e)
        {
            string _ten = txtNhanVien.Text;
            int _mapb = int.Parse(drPhongBan.SelectedValue);

            int _thang = int.Parse(drThang.SelectedValue);
            int _nam = int.Parse(txtNam.Text);

            LoadReport(_thang, _nam, _ten, _mapb);
        }

        protected void btnAll_Click(object sender, EventArgs e)
        {
            string _ten = "";
            int _mapb = 0;

            int _thang = int.Parse(drThang.SelectedValue);
            int _nam = int.Parse(txtNam.Text);

            LoadReport(_thang, _nam, _ten, _mapb);
        }

        //protected void ExportToExcel_Click(object sender, EventArgs e)
        //{
        //    try
        //    {
        //        //DataTable dt1 = (DataTable)ViewState["dtList"];
        //        DataTable dt1 = ConvertToDataTable();
        //        if (dt1 == null)
        //        {
        //            lbThongBao.Text = "Không có dữ liệu để xuất!";
                    
        //        }
        //        string Path = "D:\\Luong_" + DateTime.Now.Day.ToString() + "_" + DateTime.Now.Month.ToString() + "_" + DateTime.Now.Year.ToString() + ".xls";
        //        FileInfo FI = new FileInfo(Path);
        //        StringWriter stringWriter = new StringWriter();
        //        HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWriter);
        //        DataGrid DataGrd = new DataGrid();
        //        DataGrd.DataSource = dt1;
        //        DataGrd.DataBind();

        //        DataGrd.RenderControl(htmlWrite);
        //        string directory = Path.Substring(0, Path.LastIndexOf("\\"));// GetDirectory(Path);
        //        if (!Directory.Exists(directory))
        //        {
        //            Directory.CreateDirectory(directory);
        //        }

        //        System.IO.StreamWriter vw = new System.IO.StreamWriter(Path, true);
        //        stringWriter.ToString().Normalize();
        //        vw.Write(stringWriter.ToString());
        //        vw.Flush();
        //        vw.Close();
        //        WriteAttachment(FI.Name, "application/vnd.ms-excel", stringWriter.ToString());
        //    }
        //    catch (Exception ex)
        //    {
        //        //throw new Exception(ex.Message);
        //    }
        //}

        public static void WriteAttachment(string FileName, string FileType, string content)
        {
            HttpResponse Response = System.Web.HttpContext.Current.Response;
            Response.ClearHeaders();
            Response.AppendHeader("Content-Disposition", "attachment; filename=" + FileName);
            Response.ContentType = FileType;
            Response.Write(content);
            Response.End();
        }

        //protected DataTable ConvertToDataTable()
        //{
        //    DataTable dt = new DataTable();

        //    // add the columns to the datatable            
        //    if (dgvBangLuong.HeaderRow != null)
        //    {

        //        for (int i = 0; i < dgvBangLuong.HeaderRow.Cells.Count; i++)
        //        {
        //            dt.Columns.Add(dgvBangLuong.HeaderRow.Cells[i].Text);
        //        }
        //    }

        //    //  add each of the data rows to the table
        //    foreach (GridViewRow row in dgvBangLuong.Rows)
        //    {
        //        DataRow dr;
        //        dr = dt.NewRow();

        //        for (int i = 0; i < row.Cells.Count; i++)
        //        {
        //            dr[i] = row.Cells[i].Text.Replace("&nbsp;", "");
        //        }
        //        dt.Rows.Add(dr);
        //    }

        //    //  add the footer row to the table
        //    if (dgvBangLuong.FooterRow != null)
        //    {
        //        DataRow dr;
        //        dr = dt.NewRow();

        //        for (int i = 0; i < dgvBangLuong.FooterRow.Cells.Count; i++)
        //        {
        //            dr[i] = dgvBangLuong.FooterRow.Cells[i].Text.Replace("&nbsp;", "");
        //        }
        //        dt.Rows.Add(dr);
        //    }

        //    return dt;
        //}
    }
}